package xin.printk.android.module.plan;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import xin.printk.android.BasicFragment;
import xin.printk.android.R;
import xin.printk.android.data.entity.Plan;
import xin.printk.android.utils.LogUtils;
import xin.printk.android.utils.RequestCallback;
import xin.printk.android.view.adapter.AbstractRecyclerAdapter;
import xin.printk.android.view.adapter.UndonePlanAdapter;

public class UndonePlanFragment extends BasicFragment implements PlanContract.View, SwipeRefreshLayout.OnRefreshListener {

    private FloatingActionButton mNewPlanFAB;

    private RecyclerView mUndonePlanList;
    private UndonePlanAdapter mPlanAdapter;
    private List<Plan> mUndonePlans;

    private SwipeRefreshLayout mRefreshLayout;

    private int mPosition;
    private static final int REQUEST_CODE = 1000;
    private static final int ADD_REQUEST_CODE = 1001;
    private static final int UPDATE_RESPONSE_CODE = PlanDetailActivity.UPDATE_RESPONSE_CODE;
    private static final int STATUS_CHANGE_RESPONSE_CODE = PlanDetailActivity.STATUS_CHANGE_RESPONSE_CODE;
    private static final int DELETE_RESPONSE_CODE = PlanDetailActivity.DELETE_RESPONSE_CODE;

    private int mPage = 0;

    private PlanContract.Presenter mPresenter;

    @Override
    protected int bindLayoutResId() {
        return R.layout.fragment_undone_plan;
    }

    @Override
    protected void onInit() {
        mNewPlanFAB = findView(R.id.fab_new_plan);
        mRefreshLayout = findView(R.id.sr_undone_plan_refresh);
        mRefreshLayout.setColorSchemeColors(getThemeColor());
        mRefreshLayout.setOnRefreshListener(this);
        mUndonePlanList = findView(R.id.rv_undone_plan_list);
        mUndonePlans = new ArrayList<>();
        mPlanAdapter = new UndonePlanAdapter(getContext(), mUndonePlans);
        mUndonePlanList.setAdapter(mPlanAdapter);
        mUndonePlanList.setLayoutManager(new LinearLayoutManager(getContext()));
        mPlanAdapter.setOnItemClickListener(new AbstractRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                mPosition = position;
                Intent intent = new Intent(getActivity(), PlanDetailActivity.class);
                intent.putExtra("plan_id", mUndonePlans.get(position).getId());
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
        mNewPlanFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddPlanActivity.class);
                startActivityForResult(intent, ADD_REQUEST_CODE);
            }
        });

        mUndonePlanList.addOnScrollListener(new RecyclerView.OnScrollListener() {

            int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (mPlanAdapter.getItemCount() < 10) {
                    return;
                }
                if (mPage > mPlanAdapter.getItemCount()) {
                    return;
                }
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem == mPlanAdapter.getItemCount() - 1) {
                    mPresenter.loadMore();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });

        mPresenter = new PlanPresenter(this);
    }

    @Override
    protected void onExecute(Bundle savedInstanceState) {
        mRefreshLayout.setRefreshing(true);
        mPresenter.refresh();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.d("UndonePlanFragment requestCode=" + requestCode + " resultCode=" + resultCode);
        if (requestCode == REQUEST_CODE) {
            if (resultCode == UPDATE_RESPONSE_CODE) {
                mPresenter.refresh();
            } else if (resultCode == STATUS_CHANGE_RESPONSE_CODE || resultCode == DELETE_RESPONSE_CODE) {
                LogUtils.d("undone remove position " + mPosition);
                mUndonePlans.remove(mPosition);
                mPlanAdapter.notifyItemRemoved(mPosition);
                mPlanAdapter.notifyDataSetChanged();
            }
        } else if (requestCode == ADD_REQUEST_CODE) {
            mPresenter.refresh();
        }
    }

    @Override
    public void onRefresh() {
        mPresenter.refresh();
    }

    @Override
    public void resetPage() {
        mPage = 0;
    }

    @Override
    public void morePage() {
        mPage += 10;
    }

    @Override
    public int getPageIndex() {
        return mPage;
    }

    @Override
    public void notifyUpdate(List<Plan> list) {
        mUndonePlans.clear();
        mUndonePlans.addAll(list);
        mPlanAdapter.notifyDataSetChanged();
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void notifyMore(List<Plan> list) {
        mUndonePlans.addAll(list);
        mPlanAdapter.notifyDataSetChanged();
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void showError(String message) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
    }

    @Override
    public int getState() {
        return 0;
    }
}
